<?php

namespace App\Eloquent\Ygt;

use Framework\BaseClass\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;

class StockIn extends DbEloquent
{
    use SoftDeletes;
    protected $table = 'ygt_stockin';
    protected $dates = ['deleted_at'];

    public function addStock($saveData)
    {
        $id = self::insertGetId ($saveData);
        return $id;

        $StockInInfo = self::firstOrNew(['stock_id' => $saveData['stock_id']]);
        if ($StockInInfo->id) xThrow(ERR_CLIENT_USER_ALREADY_EXISTS);

        if($this->saveStockIn($StockInInfo, $saveData)){
            return $StockInInfo->id;
        }else{
            return false;
        }
    }

    private function saveStockIn(self $StockInInfo, array $saveData)
    {
        $StockInInfo->fill($saveData);
        return $StockInInfo->save();
    }

    //入库流水的curl,,,,,,,,!!!!!!!!!!!!!
    //采购入库的时候,自动走接口
    public static function waterAddPostCurl($productList,$header)
    {
        $url            = env('APP_URL');
        $url            .= '/api/service/stock/water-add';
        foreach($productList as $key=>$val)
        {
            $param      = 'product_id='.$val['product_id'];//材料id
            $param      .= '&number='.$val['number'];//材料数量
            $param      .= '&unit_price='.$val['unit_price'];//单价
            $param      .= '&distribution_company='.$val['distribution_company'];//单位
            $param      .= '&distribution_time='.$val['distribution_time'];//配送时间--时间戳
            $param      .= '&contact_number='.$val['contact_number'];//联系方式
            $param      .= '&remark='.$val['remark'];//备注
            $param      .= '&place_name='.$val['place_name'];//堆位
            $param      .= '&is_print'.$val['is_print'];//是否打印--固定0
            $param      .= '&non_arr='.$val['non_arr'];//自定义的字段
            //non_arr   格式是用json_decode(htmlspecialchars_decode($non_arr), true)可解析的格式
            //          即之前和app统一的那种格式
            //之下是有值时候的例子
//            $param      .= 'non_arr="["';
//            $param      .= '{';
//            $param      .= '"field_name":"字段名称",';
//            $param      .= '"field_value":"字段值",';
//            $param      .= '"max_value":"最大值",';
//            $param      .= '"min_value":"最小值",';
//            $param      .= '"unit":"字段单位",';
//            $param      .= '"type":"2"';//固定传2
//            $param      .= '},';
//            $param      .= '{}';//多个
//            $param      .= 'non_arr="]"';
            $curl = curl_init();
            curl_setopt($curl, CURLOPT_URL, $url);
            curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
            curl_setopt($curl, CURLOPT_POST, 1);
            curl_setopt($curl, CURLOPT_HEADER, 1);
            curl_setopt($curl, CURLOPT_HTTPHEADER, $header);
            curl_setopt($curl, CURLOPT_POSTFIELDS, $param);
            $curl_result = curl_exec($curl);
            curl_close($curl);
        }
    }

}
